<template>
  <div class="login-body">
    <t-form class="login-form" ref="form" :rules="rules" :data="formData" :colon="true" :label-width="0" @submit="onSubmit">
      <t-form-item>
        <div class="admin-name">Vue Tdesign Admin</div>
      </t-form-item>
      <t-form-item name="account">
        <t-input v-model="formData.account" clearable placeholder="请输入账户名">
          <template #prefix-icon>
            <desktop-icon />
          </template>
        </t-input>
      </t-form-item>

      <t-form-item name="password">
        <t-input v-model="formData.password" type="password" clearable placeholder="请输入密码">
          <template #prefix-icon>
            <lock-on-icon />
          </template>
        </t-input>
      </t-form-item>

      <t-form-item>
        <t-button theme="primary" type="submit" block>登录</t-button>
      </t-form-item>
    </t-form>
    <Bubble v-for="(item, index) in bubbleList" v-bind="item" :key="index"></Bubble>
  </div>
</template>
<script setup>
import { reactive } from 'vue'
import { MessagePlugin } from 'tdesign-vue-next'
import { DesktopIcon, LockOnIcon } from 'tdesign-icons-vue-next'
import { useRouter } from 'vue-router'
import Bubble from '../../components/bubble.vue'
import request from '@/request'
import { useUserInfo } from '../../store/user'
import { useMenu } from '../../store/menu'
const menu = useMenu()
const router = useRouter()
const formData = reactive({
  account: 'superadmin',
  password: '123456'
})

const rules = {
  account: [{ required: true, message: '姓名必填', type: 'error' }],
  password: [{ required: true, message: '密码必填', type: 'error' }]
}

const user = useUserInfo()
const onSubmit = ({ validateResult, firstError }) => {
  if (validateResult === true) {
    request({
      url: '/login',
      method: 'post',
      data: formData
    }).then(res => {
      MessagePlugin.success('登录成功')
      user.setUserInfo(res.userInfo)
      sessionStorage.setItem('tokenId', 'abcdefg')
      router.push(menu.menu[0].path)
    }).catch(err => {
      console.log(err)
      MessagePlugin.error(err)
    })
  }
}

//  气泡
const bubbleList = [
  {
    size: '100px',
    delay: '2s',
    style: {
      left: '20px',
      animationDuration: '4s'
    },
    sway: 80
  },
  {
    size: '60px',
    delay: '3s',
    style: {
      left: '100px',
      animationDuration: '8s'
    }
  },
  {
    size: '80px',
    delay: '4s',
    style: {
      left: '200px',
      animationDuration: '6s'
    },
    sway: 160
  },
  {
    size: '120px',
    delay: '5s',
    style: {
      left: '450px'
    }
  },
  {
    size: '140px',
    delay: '6s',
    style: {
      left: '700px'
    },
    sway: 300
  },
  {
    size: '160px',
    delay: '7s',
    style: {
      left: '1000px'
    }
  },
  {
    size: '160px',
    delay: '3s',
    style: {
      left: '1300px'
    },
    sway: 100
  },
  {
    size: '100px',
    delay: '1s',
    style: {
      left: '1200px'
    }
  },
  {
    size: '160px',
    delay: '5s',
    style: {
      left: '100px'
    }
  }
]
</script>
<style scoped>
.login-body {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  /* background: url('../../assets/bg.jpg') no-repeat; */
  background-image: radial-gradient(farthest-side at 0 0, rgba(255, 255, 255, 0.5), rgba(25, 146, 177, 0.5));
  background-size: cover;
}

.login-form {
  padding: 50px 30px;
  border: 1px solid #ccc;
  border-radius: 8px;
  width: 360px;
  background: var(--td-bg-color-container);
  z-index: 9;
}

.admin-name {
  flex: 1;
  text-align: center;
  font-size: 20px;
  color: var(--td-text-color-primary);
}
</style>
